<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ResourceUserReadBookDay;
use App\Models\UserInfo;
use App\Validate\UserInfoValidate;
use Illuminate\Support\Facades\DB;

/**
 * 用户管理模块
 */
class UserInfoController extends CommonController
{
    public $model = null;
    public $validate = null;

    public function __construct()
    {
        parent::__construct();

        $this->model = new UserInfo();
        $this->validate = new UserInfoValidate();
    }

    /**
     * 用户列表
     * @param page 默认为 1
     * @param limit 限制条数 默认为 10
     * @param keywords 检索内容
     * @param start_time 开始时间
     * @param end_time 结束时间
     * @param sort 排序方式   1 默认排序（默认）  2 积分倒序
     */
    public function lists()
    {
        $page = intval($this->request->page) ?: 1;
        $limit = $this->request->limit ?: 10;
        $keywords = $this->request->keywords;
        $start_time = $this->request->start_time;
        $end_time = $this->request->end_time;
        $sort = $this->request->input('sort', 1);
        //  $sort = $sort == 2 ? 'score' : 'create_time';

        $res = $this->model->lists($keywords, $start_time, $end_time, $sort, $limit);

        if ($res['total'] == 0 || !$res['data']) {
            return $this->returnApi(203, "暂无数据");
        }
        $res = $this->disPageData($res);
        $res['data'] = $this->addSerialNumber($res['data'], $page, $limit);

        //获取注册人数
        $res['day_register_num'] = $this->model->getUserNumber(1, date('Y-m-d 00:00:00'), date('Y-m-d 23:59:59'));//今日注册量
        $res['month_register_num'] = $this->model->getUserNumber(1, date('Y-m-01 00:00:00'), date('Y-m-d 23:59:59'));
        $res['year_register_num'] = $this->model->getUserNumber(1, date('Y-01-01 00:00:00'), date('Y-m-d 23:59:59'));
        $res['total_register_num'] = $this->model->getUserNumber(1);

        return $this->returnApi(200, "查询成功", true, $res);
    }
    /**
     * 清空违规次数
     * @param user_id 读者证id
     * @param node  0 全部清空  1 活动 2 预约
     */
    public function clearViolate()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('clear_violate')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        DB::beginTransaction();
        try {
            $this->model->clearViolate($this->request->user_id, $this->request->node);
            DB::commit();
            return $this->returnApi(200, "清空成功");
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->returnApi(202, "清空失败");
        }
    }
}
